How to Loop or Start a Batch File Over After It Has Completed

您所在的位置:网站首页 :start start cmd goto start怎么关 How to Loop or Start a Batch File Over After It Has Completed

How to Loop or Start a Batch File Over After It Has Completed

2024-07-12 12:53| 来源: 网络整理| 查看: 265

How to loop or start a batch file over after it has completed Updated: 11/13/2018 by Computer Hope

You can use the goto command in a batch file to "branch" the execution of your script, skipping to another section of the program. If you skip to a later part of the program, you can bypass lines of the script. If you skip to a previous part of the program, you can create a simple loop.

The following are examples of some ways to use goto in a Windows batch file.

@echo off cls :start echo Example of a loop goto start

In this first example, the computer prints "Example of a loop" over and over until you terminate the file. To cancel this example press: Ctrl+C.

@echo off cls :start echo Example of a loop pause goto start

Next, adding the pause statement before the goto line prompts the user to press any key before looping the batch file. Adding pause allows the user to run the batch when they're ready.

@echo off cls :start echo Example of a loop set choice= set /p choice="Do you want to restart? Press 'y' and enter for Yes: " if not '%choice%'=='' set choice=%choice:~0,1% if '%choice%'=='y' goto start

Finally, in this last example and most recommend method, the user would be prompted if they want to rerun the batch file. Pressing "y" would use the goto command and go back to start and rerun the batch file. Pressing any other key would exit the batch file. The above code is for Windows 2000, XP, and later users if you're running earlier Windows 98 or earlier you'd need to use the choice command.

Note

Replacing the "echo Example of a loop" line with your batch file allows any of your batch files to loop or rerun.

Related information Our goto page and set page has the complete syntax of these commands. See the loop definition for further information and related links on loops. Batch file help.


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3